<!-- 图片上传 -->
<template>
  <div class='upload'>
    <template v-if="disabled">
      <el-image :src="item.url" class="img" v-for="item in fileList" :key="item.url" :preview-src-list="[item.url]"></el-image>
    </template>

    <template v-else>
      <!-- 上传一张 -->
      <template v-if="one">
        <div v-if="fileList.length" class="img action">
          <img :src="fileList[0].url" class="img">
          <label class="label">
            <i class="success el-icon-check"></i>
          </label>
          <div class="action_item flex items-center justify-center">
            <i class="el-icon-zoom-in cursor-pointer" @click="preview"></i>
            <i class="el-icon-delete cursor-pointer" style="margin-left: 15px;" @click="remove"></i>
          </div>
        </div>

        <el-upload class="avatar-uploader" v-else :action="uploadAction" :headers="headers" :show-file-list="false" :on-success="handleSuccess">
          <i class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </template>

      <!-- 多张上传 -->
      <template v-else>
        <el-upload :action="uploadAction" :headers="headers" list-type="picture-card" :file-list="fileList" :on-preview="preview" :on-remove="remove" :on-success="(response, file, fileList) => handleSuccess(response, file, fileList,true)">
          <i class="el-icon-plus"></i>
        </el-upload>
      </template>
    </template>

    <el-dialog :visible.sync="dialogVisible" append-to-body>
      <img class="w-full" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { ref, toRefs, watch } from '@vue/composition-api'
import config from '@/config'
import mixin from '@/utils/mixin'

const { get } = mixin()

let stop = null
// 处理数据
const fileList = ref([])
function splitData({ value, one }) {

  stop = watch(value, splitFn)

  function splitFn(n) {
    if (!n) {
      fileList.value = []
      return
    }
    n.split(",").forEach(item => {
      fileList.value.push({
        url: item
      })
    })
    if (one.value) return
    stop()
  }

  return {
    fileList
  }
}

// 事件处理
function handle(emit) {
  // 上传成功
  function handleSuccess(response, file, fileList, flag) {
    if (flag) stop() // 如果是多图上传，停止watch监听
    dataMmit(fileList)
  }
  // 移除
  function remove() {
    dataMmit([])
  }
  // 预览
  const dialogVisible = ref(false)
  const dialogImageUrl = ref('')
  function preview(file) {
    if (file.url) {
      dialogImageUrl.value = file.url
    } else {
      dialogImageUrl.value = fileList.value[0].url
    }
    dialogVisible.value = true
  }
  // 事件下发
  function dataMmit(fileList) {
    let arr = fileList.map((item) => {
      if (item.status === "success") {
        return item?.response?.url || item.url
      }
    });
    emit('input', arr.join(","))
    emit("success", arr)
  }
  return {
    handleSuccess,
    remove,
    preview,
    dialogVisible,
    dialogImageUrl
  }
}


export default {
  name: "Upload",
  props: {
    // 展示的数据
    value: {
      type: String,
      default: '',
    },
    // 是否上传
    disabled: {
      type: Boolean,
      default: false,
    },
    // 只上传一张
    one: {
      type: Boolean,
      default: false,
    }
  },
  setup(props, { emit }) {
    return {
      uploadAction: config.url + "/sys/oss/upload",
      headers: { DT_TOKEN: get.token },
      ...splitData(toRefs(props)),
      ...handle(emit),
    }
  }
}

</script>

<style lang='scss' scoped>
.upload {
  .avatar-uploader {
    ::v-deep .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    ::v-deep .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 148px;
      height: 148px;
      line-height: 148px;
      text-align: center;
    }
  }

  .img {
    width: 148px;
    height: 148px;
    border-radius: 6px;
    margin: 0 8px 8px 0;
    overflow: hidden;
    border: 1px solid #c0ccda;
    &:not(:last-child) {
      margin-right: 8rpx;
    }
  }
  .action {
    position: relative;

    &:hover .label {
      display: none;
    }
    &:hover .action_item {
      opacity: 1;
    }

    .label {
      position: absolute;
      right: -17px;
      top: -7px;
      width: 46px;
      height: 26px;
      background: #13ce66;
      text-align: center;
      transform: rotate(45deg);
      box-shadow: 0 0 1pc 1px rgb(0 0 0 / 20%);

      .success {
        color: #fff;
        font-size: 12px;
        margin-top: 12px;
        transform: rotate(-45deg);
      }
    }

    .action_item {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, 0.5);
      transition: opacity 0.3s;
    }
  }
}
</style>